﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;

public partial class chexiaoRecordsList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        } 
        string JudgePower = Session["power"].ToString();
        if (JudgePower != "超级管理员")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
        if (!IsPostBack)
        {
            this.bind();
        }
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select RID as 编号 , RGID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌,  RWID as 仓库编号,RQuantity as 数量,RHandler as 收货人,RDate as 日期,RifAOG as 到货数量,RComfirmer as 确认人,RComfirmedDate as 确认日期,bianhao as 项目编号,PName as 项目名,PID as id from Projects,RecordsIN, Goods where RecordsIN.RGID=Goods.GID and RecordsIN.RPID=Projects.PID order by RID desc";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "编号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();
        myConn.Close();
    }


    public bool IsNumberic(string str)
    {

        Regex reg = new Regex("^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$");
        Match ma = reg.Match(str);
        if (ma.Success)
        {
            //是数字  
            return true;
        }
        else
        {
            //不是数字    
            return false;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim().ToString() == "")
        {
            Response.Write("<script>alert('货品名不能为空!')</script>");
        }
        else
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlStr = "select RID as 编号 , RGID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌, RWID as 仓库编号,RQuantity as 数量,RHandler as 收货人,RDate as 日期,RifAOG as 到货确认,RComfirmer as 确认人,RComfirmedDate as 确认日期,bianhao as 项目编号,PName as 项目名,PID as id  from Projects,RecordsIN, Goods where RecordsIN.RGID=Goods.GID and RecordsIN.RPID=Projects.PID and GCHName like '%" + TextBox1.Text + "%' order by RID desc";
            SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);
            GridView1.DataSource = myDs;
            GridView1.DataKeyNames = new string[] { "编号" };
            GridView1.DataBind();
            myDa.Dispose();
            myDs.Dispose();
            myConn.Close();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text.Trim().ToString() == "")
        {
            Response.Write("<script>alert('项目名或项目号不能为空!')</script>");
        }
        else
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlStr = "select RID as 编号 , RGID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌,  RWID as 仓库编号,RQuantity as 数量,RHandler as 收货人,RDate as 日期,RifAOG as 到货确认,RComfirmer as 确认人,RComfirmedDate as 确认日期,bianhao as 项目编号,PName as 项目名,PID as id  from RecordsIN, Goods,Projects where RecordsIN.RGID=Goods.GID and RecordsIN.RPID=Projects.PID  and (PName like '%" + TextBox2.Text + "%' or bianhao like '%" + TextBox2.Text + "%') order by RID desc";
            SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);
            GridView1.DataSource = myDs;
            GridView1.DataKeyNames = new string[] { "编号" };
            GridView1.DataBind();
            myDa.Dispose();
            myDs.Dispose();
            myConn.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedValue != null && TextBox3.Text!="")
        {
            string shuliang = TextBox3.Text.Trim().ToString();
            if (IsNumberic(shuliang))
            {
                SqlConnection myConn = GetConnection();
                myConn.Open();
                string sqlStr = "select RID , RGID ,GTYPEName ,GCHName ,GXinhao ,GBrand ,  RWID ,RQuantity ,RHandler ,RDate ,RifAOG,RComfirmer ,RComfirmedDate ,bianhao,PName ,PID  from Projects,RecordsIN, Goods where RecordsIN.RGID=Goods.GID and RecordsIN.RPID=Projects.PID and RecordsIN.RID='" + GridView1.SelectedValue.ToString() + "'";
                SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
                myCmd.ExecuteNonQuery();
                SqlDataReader read = myCmd.ExecuteReader();
                read.Read();
                string pid = read["PID"].ToString();
                string rgid = read["RGID"].ToString();
                string SUM = read["RifAOG"].ToString();
                int rquan = Convert.ToInt16(read["RQuantity"].ToString());
                read.Close();
                if (!(Convert.ToInt16(shuliang) > Convert.ToInt16(SUM)))
                {
                    sqlStr = "update  ProjectsQuantity set PQuantity=PQuantity-'" + Convert.ToInt16(shuliang) + "'where PID='" + pid + "' and PGID='" + rgid + "'";
                    myCmd = new SqlCommand(sqlStr, myConn);
                    myCmd.ExecuteNonQuery();


                    sqlStr = "update RecordsIN SET RifAOG=RifAOG-'" + Convert.ToInt16(shuliang) + "',RComfirmer=null,RComfirmedDate=null  where RID='" + GridView1.SelectedValue.ToString() + "'";
                    myCmd = new SqlCommand(sqlStr, myConn);
                    myCmd.ExecuteNonQuery();

                    sqlStr = "update Store set SQuantity=SQuantity-'" + Convert.ToInt16(shuliang) + "' where SGID='" + rgid + "'";
                    myCmd = new SqlCommand(sqlStr, myConn);
                    myCmd.ExecuteNonQuery();
                    myConn.Close();
                    Response.Redirect("~/chexiaoRecordsList.aspx?");
                }
                else
                {
                    myConn.Close();
                    Response.Write("<script>alert('填写的数量超过上限!')</script>");
                }
            }
            else
                Response.Write("<script>alert('填写的数量错误!')</script>");
        }
        else
            Response.Write("<script>alert('未选择要撤销确认的选项或未填写数量!')</script>");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
}